<template>
  <s-layout title="加入文明实践团队" navbar="normal" :bgStyles="{ color: '#f5f5f5' }">
    <!-- Search Bar -->
    <view class="search-bar">
      <text class="search-icon">🔍</text>
      <input
        v-model="state.searchKeyword"
        class="search-input"
        placeholder="搜索团队名称或服务类型"
        @confirm="handleSearch"
      />
      <text class="search-btn" @tap="handleSearch">搜索</text>
    </view>

    <!-- Category Filter Tabs -->
    <scroll-view class="filter-tabs" scroll-x show-scrollbar="false">
      <view class="filter-tabs-inner">
        <view
          v-for="(filter, index) in state.filters"
          :key="index"
          :class="['filter-tab', { active: state.activeFilter === index }]"
          @tap="handleFilterChange(index)"
        >
          {{ filter }}
        </view>
      </view>
    </scroll-view>

    <!-- Featured Team Section -->
    <view class="featured-section">
      <view class="featured-header">
        <text>⭐</text>
        <text class="featured-title">推荐团队</text>
      </view>
      <view class="featured-team" @tap="handleTeamClick(state.featuredTeam)">
        <view class="team-badge">热门</view>
        <view class="featured-content">
          <view class="featured-avatar">{{ state.featuredTeam.emoji }}</view>
          <view class="featured-info">
            <text class="featured-team-name">{{ state.featuredTeam.name }}</text>
            <text class="featured-team-desc">{{ state.featuredTeam.desc }}</text>
            <view class="featured-stats">
              <text>👥 {{ state.featuredTeam.members }}人</text>
              <text>📅 成立{{ state.featuredTeam.years }}年</text>
              <text>⭐ {{ state.featuredTeam.rating }}分</text>
            </view>
            <button class="join-btn" @tap.stop="handleJoinTeam(state.featuredTeam)">
              申请加入
            </button>
          </view>
        </view>
      </view>
    </view>

    <!-- Team List -->
    <view class="team-list">
      <view
        v-for="team in state.filteredTeams"
        :key="team.id"
        class="team-card"
        @tap="handleTeamClick(team)"
      >
        <view class="team-header">
          <view class="team-info">
            <view class="team-avatar">{{ team.emoji }}</view>
            <view class="team-details">
              <text class="team-name">{{ team.name }}</text>
              <view class="team-type">{{ team.type }}</view>
              <text class="team-desc">{{ team.desc }}</text>
            </view>
          </view>
          <view
            :class="['team-status', team.recruiting ? 'status-recruiting' : 'status-full']"
          >
            {{ team.recruiting ? '招募中' : '人员已满' }}
          </view>
        </view>
        <view class="team-footer">
          <view class="team-stats">
            <view class="stat">
              <text>👥 {{ team.members }}人</text>
            </view>
            <view class="stat">
              <text>📍 {{ team.location }}</text>
            </view>
            <view class="stat">
              <text>⭐ {{ team.rating }}分</text>
            </view>
          </view>
          <view class="team-action">
            <button class="btn btn-secondary" @tap.stop="handleContactTeam(team)">
              联系
            </button>
            <button
              :class="['btn', team.recruiting ? 'btn-primary' : 'btn-secondary']"
              @tap.stop="team.recruiting ? handleJoinTeam(team) : handleWaitingList(team)"
            >
              {{ team.recruiting ? '加入' : '候补' }}
            </button>
          </view>
        </view>
      </view>
    </view>

    <!-- Load More -->
    <view class="load-more">
      <button class="load-more-btn" @tap="handleLoadMore">查看更多团队</button>
    </view>

    <!-- My Applications Button (Fixed) -->
    <view class="fab-btn" @tap="handleShowMyApplications">
      <text class="fab-icon">📋</text>
    </view>
  </s-layout>
</template>

<script setup>
import { reactive, computed } from 'vue'
import sheep from '@/sheep'

const state = reactive({
  searchKeyword: '',
  activeFilter: 0,
  filters: ['全部', '志愿服务', '文艺团队', '教育培训', '医疗健康', '科技服务'],
  featuredTeam: {
    id: 0,
    name: '海城环保志愿者协会',
    emoji: '🌱',
    type: '志愿服务',
    desc: '致力于环境保护宣传和绿色生活推广，组织各类环保实践活动',
    members: 156,
    years: 3,
    rating: 4.8,
    location: '海城区',
    recruiting: true
  },
  teamList: [
    {
      id: 1,
      name: '海城文艺演出团',
      emoji: '🎭',
      type: '文艺团队',
      desc: '专业文艺演出团队，擅长舞蹈、歌曲、戏曲等多种表演形式，为社区提供精彩的文化服务',
      members: 45,
      location: '海城区',
      rating: 4.6,
      recruiting: true,
      category: '文艺团队'
    },
    {
      id: 2,
      name: '社区医疗服务队',
      emoji: '🏥',
      type: '医疗健康',
      desc: '由专业医护人员组成，定期开展健康义诊、急救培训、健康知识宣传等活动',
      members: 28,
      location: '海城区',
      rating: 4.9,
      recruiting: true,
      category: '医疗健康'
    },
    {
      id: 3,
      name: '青少年教育志愿团',
      emoji: '📚',
      type: '教育培训',
      desc: '专注青少年教育辅导，提供课业辅导、兴趣培养、心理健康指导等服务',
      members: 50,
      location: '海城区',
      rating: 4.7,
      recruiting: false,
      category: '教育培训'
    },
    {
      id: 4,
      name: '敬老助残服务队',
      emoji: '👴',
      type: '志愿服务',
      desc: '关爱老年人和残障人士，提供生活帮助、陪伴服务、康复指导等温暖服务',
      members: 32,
      location: '海城区',
      rating: 4.8,
      recruiting: true,
      category: '志愿服务'
    },
    {
      id: 5,
      name: '数字助老志愿队',
      emoji: '💻',
      type: '科技服务',
      desc: '帮助老年人学习使用智能手机、电脑等数字设备，消除数字鸿沟',
      members: 18,
      location: '海城区',
      rating: 4.5,
      recruiting: true,
      category: '科技服务'
    },
    {
      id: 6,
      name: '法律援助志愿团',
      emoji: '⚖️',
      type: '志愿服务',
      desc: '由专业律师组成，为社区居民提供免费法律咨询和援助服务',
      members: 12,
      location: '海城区',
      rating: 4.9,
      recruiting: true,
      category: '志愿服务'
    }
  ]
})

const filteredTeams = computed(() => {
  let result = state.teamList

  // Filter by category
  if (state.activeFilter > 0) {
    const category = state.filters[state.activeFilter]
    result = result.filter((t) => t.category === category)
  }

  // Filter by search keyword
  if (state.searchKeyword) {
    result = result.filter(
      (t) =>
        t.name.includes(state.searchKeyword) ||
        t.type.includes(state.searchKeyword) ||
        t.desc.includes(state.searchKeyword)
    )
  }

  return result
})

state.filteredTeams = filteredTeams

const handleSearch = () => {
  console.log('Searching:', state.searchKeyword)
}

const handleFilterChange = (index) => {
  state.activeFilter = index
}

const handleTeamClick = (team) => {
  // Navigate to team detail page
  sheep.$router.go(`/pages/cmt/team-detail?id=${team.id}`)
}

const handleJoinTeam = (team) => {
  uni.showModal({
    title: `申请加入团队：${team.name}`,
    content: '申请流程：\n1. 填写申请表\n2. 团队负责人审核\n3. 面试或试用期\n4. 正式加入',
    confirmText: '立即申请',
    success: (res) => {
      if (res.confirm) {
        // Navigate to join application page
        sheep.$router.go(`/pages/cmt/team-join?id=${team.id}`)
      }
    }
  })
}

const handleContactTeam = (team) => {
  uni.showModal({
    title: '联系团队',
    content: '联系方式：\n电话：138-0000-0000\n微信：team_contact\n邮箱：team@example.com\n\n建议先通过平台申请加入，负责人会主动联系您。',
    showCancel: false
  })
}

const handleWaitingList = (team) => {
  uni.showModal({
    title: '加入候补列表',
    content: `该团队目前人员已满，您可以：\n1. 加入候补列表\n2. 有名额时优先通知\n3. 关注团队动态\n\n是否确认加入候补？`,
    confirmText: '确认加入',
    success: (res) => {
      if (res.confirm) {
        uni.showToast({
          title: '已加入候补列表',
          icon: 'success'
        })
      }
    }
  })
}

const handleLoadMore = () => {
  uni.showToast({
    title: '正在加载更多',
    icon: 'loading'
  })
  // TODO: Load more teams from API
}

const handleShowMyApplications = () => {
  // Navigate to my applications page
  sheep.$router.go('/pages/cmt/my-applications')
}
</script>

<style lang="scss" scoped>
.search-bar {
  background: white;
  margin: 30rpx;
  border-radius: 40rpx;
  padding: 20rpx 30rpx;
  display: flex;
  align-items: center;
  gap: 20rpx;
  box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.1);

  .search-icon {
    font-size: 32rpx;
    color: #666;
  }

  .search-input {
    flex: 1;
    font-size: 28rpx;
  }

  .search-btn {
    color: #ff6b00;
    font-size: 28rpx;
    font-weight: bold;
  }
}

.filter-tabs {
  background: white;
  margin: 0 30rpx 30rpx;
  border-radius: 32rpx;
  padding: 10rpx;
  box-shadow: 0 4rpx 24rpx rgba(0, 0, 0, 0.08);

  .filter-tabs-inner {
    display: inline-flex;
    white-space: nowrap;
    padding: 0 10rpx;
  }

  .filter-tab {
    flex-shrink: 0;
    text-align: center;
    padding: 10rpx 20rpx;
    border-radius: 24rpx;
    font-size: 24rpx;
    margin-right: 10rpx;
    color: #666;
    background: transparent;
    transition: all 0.3s;

    &.active {
      background: linear-gradient(135deg, #ff6b00, #ffa726);
      color: white;
      font-weight: bold;
    }
  }
}

.featured-section {
  background: white;
  margin: 0 30rpx 30rpx;
  border-radius: 32rpx;
  box-shadow: 0 4rpx 24rpx rgba(0, 0, 0, 0.08);
  overflow: hidden;

  .featured-header {
    padding: 30rpx 40rpx;
    border-bottom: 1rpx solid #f0f2f5;
    display: flex;
    align-items: center;
    gap: 16rpx;
    font-size: 28rpx;

    .featured-title {
      font-size: 32rpx;
      font-weight: bold;
      color: #333;
    }
  }

  .featured-team {
    padding: 40rpx;
    background: linear-gradient(135deg, rgba(255, 107, 0, 0.1), rgba(255, 167, 38, 0.1));
    position: relative;

    .team-badge {
      position: absolute;
      top: 30rpx;
      right: 30rpx;
      background: #ff6b00;
      color: white;
      padding: 8rpx 16rpx;
      border-radius: 24rpx;
      font-size: 20rpx;
      font-weight: bold;
    }

    .featured-content {
      display: flex;
      gap: 30rpx;
      align-items: flex-start;

      .featured-avatar {
        width: 120rpx;
        height: 120rpx;
        background: linear-gradient(135deg, #ff6b00, #ffa726);
        border-radius: 24rpx;
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 48rpx;
        flex-shrink: 0;
      }

      .featured-info {
        flex: 1;

        .featured-team-name {
          font-size: 32rpx;
          font-weight: bold;
          color: #333;
          margin-bottom: 10rpx;
          display: block;
        }

        .featured-team-desc {
          font-size: 24rpx;
          color: #666;
          margin-bottom: 16rpx;
          line-height: 1.6;
          display: block;
        }

        .featured-stats {
          display: flex;
          gap: 30rpx;
          font-size: 22rpx;
          color: #999;
          margin-bottom: 20rpx;
        }

        .join-btn {
          display: inline-block;
          background: linear-gradient(135deg, #ff6b00, #ffa726);
          color: white;
          border: none;
          padding: 12rpx 30rpx;
          border-radius: 24rpx;
          font-size: 24rpx;
          line-height: 1;
          margin-top: 16rpx;
          box-shadow: 0 4rpx 12rpx rgba(255, 107, 0, 0.3);
          transition: all 0.3s;
        }

        .join-btn::after {
          border: none;
        }
      }
    }
  }
}

.team-list {
  margin: 0 30rpx;

  .team-card {
    background: white;
    border-radius: 32rpx;
    margin-bottom: 30rpx;
    box-shadow: 0 4rpx 24rpx rgba(0, 0, 0, 0.08);
    transition: all 0.3s;
    overflow: hidden;

    .team-header {
      display: flex;
      justify-content: space-between;
      align-items: flex-start;
      padding: 30rpx 40rpx 20rpx;

      .team-info {
        display: flex;
        gap: 24rpx;
        align-items: flex-start;
        flex: 1;

        .team-avatar {
          width: 100rpx;
          height: 100rpx;
          background: linear-gradient(135deg, #ff6b00, #ffa726);
          border-radius: 24rpx;
          display: flex;
          align-items: center;
          justify-content: center;
          font-size: 40rpx;
          flex-shrink: 0;
        }

        .team-details {
          flex: 1;

          .team-name {
            font-size: 30rpx;
            font-weight: bold;
            color: #333;
            margin-bottom: 8rpx;
            display: block;
          }

          .team-type {
            background: linear-gradient(135deg, #fff3e0, #ffecb3);
            color: #ff6b00;
            padding: 4rpx 16rpx;
            border-radius: 20rpx;
            font-size: 20rpx;
            display: inline-block;
            margin-bottom: 12rpx;
          }

          .team-desc {
            font-size: 24rpx;
            color: #666;
            line-height: 1.6;
            display: -webkit-box;
            -webkit-line-clamp: 2;
            -webkit-box-orient: vertical;
            overflow: hidden;
          }
        }
      }

      .team-status {
        padding: 8rpx 16rpx;
        border-radius: 16rpx;
        font-size: 20rpx;
        font-weight: bold;
        flex-shrink: 0;

        &.status-recruiting {
          background: #d4edda;
          color: #155724;
        }

        &.status-full {
          background: #fff3cd;
          color: #856404;
        }
      }
    }

    .team-footer {
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding: 20rpx 40rpx 30rpx;
      background: #f8f9fa;

      .team-stats {
        display: flex;
        gap: 30rpx;
        font-size: 22rpx;
        color: #666;

        .stat {
          display: flex;
          align-items: center;
          gap: 6rpx;
        }
      }

      .team-action {
        display: flex;
        gap: 16rpx;

        .btn {
          padding: 12rpx 24rpx;
          border-radius: 24rpx;
          border: none;
          font-size: 22rpx;
          line-height: 1;
          transition: all 0.3s;
          box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.1);

          &.btn-primary {
            background: linear-gradient(135deg, #ff6b00, #ffa726);
            color: white;
          }

          &.btn-secondary {
            background: #fff3e0;
            color: #ff6b00;
          }
        }

        .btn::after {
          border: none;
        }
      }
    }
  }
}

.load-more {
  text-align: center;
  padding: 40rpx;

  .load-more-btn {
    display: inline-block;
    background: linear-gradient(135deg, #ff6b00, #ffa726);
    color: white;
    border: none;
    padding: 20rpx 50rpx;
    border-radius: 40rpx;
    font-size: 26rpx;
    line-height: 1;
    box-shadow: 0 4rpx 12rpx rgba(255, 107, 0, 0.3);
    transition: all 0.3s;
  }

  .load-more-btn::after {
    border: none;
  }
}

.fab-btn {
  position: fixed;
  bottom: 120rpx;
  right: 30rpx;
  width: 100rpx;
  height: 100rpx;
  background: linear-gradient(135deg, #ff6b00, #ffa726);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 8rpx 24rpx rgba(255, 107, 0, 0.3);
  z-index: 100;

  .fab-icon {
    font-size: 40rpx;
  }
}
</style>
